<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>发送一条 MySQL 查询</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.mysql-query" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">mysql_query</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">mysql_query</span> &mdash; <span class="dc-title">发送一条 MySQL 查询</span></p>

   </div>
   
 <div id="function.mysql-query-refsynopsisdiv">
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">本扩展自 PHP 5.5.0
起已废弃，并在自 PHP 7.0.0 开始被移除。应使用 <a href="book.mysqli.html" class="link">MySQLi</a>
或 <a href="ref.pdo_mysql.html" class="link">PDO_MySQL</a> 扩展来替换之。参见
<a href="mysqlinfo.api.choosing.html" class="link">MySQL：选择 API</a> 指南以及<a href="faq.databases.html#faq.databases.mysql.deprecated" class="link">相关 FAQ</a> 来获取更多信息。用以替代本函数的有：</p>
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="mysqli.query.html" class="function">mysqli_query()</a></span></li>
    <li class="member"><span class="methodname"><a href="pdo.query.html" class="methodname">PDO::query()</a></span></li>
   </ul>
  </div>
 </div>
   
 <div class="refsect1 description" id="refsect1-function.mysql-query-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>mysql_query</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$query</code></span>
   [, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$link_identifier</code><span class="initializer"> = NULL</span></span>
  ] ) : <span class="type">resource</span></div>

    <p class="para rdfs-comment">
     <span class="function"><strong>mysql_query()</strong></span>
     向与指定的 <code class="parameter">link_identifier</code> 关联的服务器中的当前活动数据库发送一条查询（不支持多条查询）。
    </p>
   </div>

   
    <div class="refsect1 parameters" id="refsect1-function.mysql-query-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">query</code></dt>

     <dd>

      <p class="para">
       SQL 查询语句
      </p>
      <p class="para">
       查询字符串不应以分号结束。
       查询中被嵌入的数据应该<a href="mysql_real_escape_string.html" class="link">正确地转义</a>。
      </p>
     </dd>

    
    <dt>
<code class="parameter">
link_identifier</code></dt>
<dd>
<p class="para">MySQL
连接。如不指定连接标识，则使用由 <span class="function"><a href="mysql_connect.html" class="function">mysql_connect()</a></span>
最近打开的连接。如果没有找到该连接，会尝试不带参数调用
<span class="function"><a href="mysql_connect.html" class="function">mysql_connect()</a></span>
来创建。如没有找到连接或无法建立连接，则会生成
<strong><code>E_WARNING</code></strong> 级别的错误。</p></dd>


   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mysql-query-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>mysql_query()</strong></span> 仅对
     SELECT，SHOW，DESCRIBE, EXPLAIN 和其他语句
     语句返回一个 <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span>，如果查询出现错误则返回 <strong><code>FALSE</code></strong>。
  </p>
  <p class="para">
   对于其它类型的 SQL
     语句，比如INSERT, UPDATE, DELETE, DROP 之类， <span class="function"><strong>mysql_query()</strong></span>
     在执行成功时返回 <strong><code>TRUE</code></strong>，出错时返回 <strong><code>FALSE</code></strong>。
  </p>
  <p class="para">
   返回的结果资源应该传递给 <span class="function"><a href="mysql_fetch_array.html" class="function">mysql_fetch_array()</a></span> 和其他函数来处理结果表,取出返回的数据。
  </p>
  <p class="para">
   假定查询成功，可以调用
     <span class="function"><a href="mysql_num_rows.html" class="function">mysql_num_rows()</a></span>
     来查看对应于 SELECT
     语句返回了多少行，或者调用
     <span class="function"><a href="mysql_affected_rows.html" class="function">mysql_affected_rows()</a></span>
     来查看对应于
     DELETE，INSERT，REPLACE 或 UPDATE
     语句影响到了多少行。
  </p>
  <p class="para">
   如果没有权限访问查询语句中引用的表时，<span class="function"><strong>mysql_query()</strong></span>
     也会返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mysql-query-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2161">
    <p><strong>Example #1 无效的查询</strong></p>
    <div class="example-contents"><p>
     以下查询语法上有错，因此
     <span class="function"><strong>mysql_query()</strong></span>
     失败并返回 <strong><code>FALSE</code></strong>。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT&nbsp;*&nbsp;WHERE&nbsp;1=1'</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$result</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'Invalid&nbsp;query:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">());<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2162">
    <p><strong>Example #2 有效的查询</strong></p>
    <div class="example-contents"><p>
     以下查询语法正确，所以 <span class="function"><strong>mysql_query()</strong></span>
     返回了一个 <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span>。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;这应该由用户提供,下面是一个示例<br /></span><span style="color: #0000BB">$firstname&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'fred'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$lastname&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'fox'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;构造查询<br />//&nbsp;这是执行&nbsp;SQL&nbsp;最好的方式<br />//&nbsp;更多例子参见&nbsp;mysql_real_escape_string()<br /></span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;firstname,&nbsp;lastname,&nbsp;address,&nbsp;age&nbsp;FROM&nbsp;friends&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;firstname='%s'&nbsp;AND&nbsp;lastname='%s'"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mysql_real_escape_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$firstname</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mysql_real_escape_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$lastname</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;执行查询<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;检查结果<br />//&nbsp;下面显示了实际发送给&nbsp;MySQL&nbsp;的查询，以及出现的错误。这对调试很有帮助。<br /></span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">$result</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$message&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Invalid&nbsp;query:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$message&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">'Whole&nbsp;query:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #0000BB">$message</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;结果的使用<br />//&nbsp;尝试&nbsp;print&nbsp;$result&nbsp;并不会取出结果资源中的信息<br />//&nbsp;所以必须至少使用其中一个&nbsp;mysql&nbsp;结果函数<br />//&nbsp;参见&nbsp;mysql_result(),&nbsp;mysql_fetch_array(),&nbsp;mysql_fetch_row()&nbsp;等。<br /></span><span style="color: #007700">while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_fetch_assoc</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'firstname'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'lastname'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'address'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'age'</span><span style="color: #007700">];<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;释放关联结果集的资源<br />//&nbsp;在脚本结束的时候会自动进行<br /></span><span style="color: #0000BB">mysql_free_result</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.mysql-query-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="mysql_connect.html" class="function" rel="rdfs-seeAlso">mysql_connect()</a> - 打开一个到 MySQL 服务器的连接</span></li>
    <li class="member"><span class="function"><a href="mysql_error.html" class="function" rel="rdfs-seeAlso">mysql_error()</a> - 返回上一个 MySQL 操作产生的文本错误信息</span></li>
    <li class="member"><span class="function"><a href="mysql_real_escape_string.html" class="function" rel="rdfs-seeAlso">mysql_real_escape_string()</a> - 转义 SQL 语句中使用的字符串中的特殊字符，并考虑到连接的当前字符集</span></li>
    <li class="member"><span class="function"><a href="mysql_result.html" class="function" rel="rdfs-seeAlso">mysql_result()</a> - 取得结果数据</span></li>
    <li class="member"><span class="function"><a href="mysql_fetch_assoc.html" class="function" rel="rdfs-seeAlso">mysql_fetch_assoc()</a> - 从结果集中取得一行作为关联数组</span></li>
    <li class="member"><span class="function"><a href="mysql_unbuffered_query.html" class="function" rel="rdfs-seeAlso">mysql_unbuffered_query()</a> - 向 MySQL 发送一条 SQL 查询，并不获取和缓存结果的行</span></li>
   </ul>
  </p>
 </div>

   
  </div></div></div></body></html>